import { Component, ViewChild } from '@angular/core';
import { ModalDirective } from 'ngx-bootstrap';
import {LazyLoadEvent} from 'primeng/components/common/api';

import { User } from '../../../core/auth/user.service';
import { CommonService } from '../../../core/common/common.service';
import { NodeComponent } from '../component/node/node.component';
import { CustomerService } from '../service/customer.service';
@Component({
  selector: 'ncloud-mycust',
  templateUrl: './mycust.html',
  styles: [`.modal-content {top: 100px;};`]
})
export class MyCust {
  public customers: Array<any>;
  public totalRecords: number;
  public nodeInfos: Array<any> = [];
  @ViewChild('nodeModal') public nodeModal: ModalDirective;
  @ViewChild('ncloudnode') public ncloudnode: NodeComponent;
  constructor(private common: CommonService, public user: User, private customerService: CustomerService) { }
  loadLazy(event: LazyLoadEvent) {
    setTimeout(() => {
      const pageNum = event.first / event.rows + 1;
      const pageSize = event.rows;
      this.customerService.getCustomers(pageNum, pageSize).subscribe(page => {
        this.customers = page.list;
        this.totalRecords = page.total;
      });
    }, 250);
  }
  public lookupNodeInfos(customer): void {
    this.customerService.getNodesByCust(customer.custId).subscribe((nodeInfos: any) => {
      if (this.ncloudnode && this.ncloudnode.nodeTab) {
        this.ncloudnode.nodeTab.remove();
      }
      this.nodeInfos = [];
      nodeInfos.forEach((n: any) => {
        n.active = true;
        if (n.isp) {
          n.isp.isDisabled = true;
        }
        n.isDisabled = true;
        this.nodeInfos.push(n);
      });
      setTimeout(_ => this.nodeModal.show(), 250);
    });
  }
}
